﻿namespace KeRui.Common
{
    using System;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Drawing.Imaging;
    using System.IO;

    public class Thumbnail
    {
        private string srcFileName;
        private Image srcImage;

        private static ImageCodecInfo GetCodecInfo(string mimeType)
        {
            ImageCodecInfo[] imageEncoders = ImageCodecInfo.GetImageEncoders();
            foreach (ImageCodecInfo info in imageEncoders)
            {
                if (info.MimeType == mimeType)
                {
                    return info;
                }
            }
            return null;
        }

        public static ImageFormat GetFormat(string name)
        {
            switch (name.Substring(name.LastIndexOf(".") + 1).ToLower())
            {
                case "jpg":
                case "jpeg":
                    return ImageFormat.Jpeg;

                case "bmp":
                    return ImageFormat.Bmp;

                case "png":
                    return ImageFormat.Png;

                case "gif":
                    return ImageFormat.Gif;
            }
            return ImageFormat.Jpeg;
        }

        public Image GetImage(int Width, int Height)
        {
            Image.GetThumbnailImageAbort callback = new Image.GetThumbnailImageAbort(Thumbnail.ThumbnailCallback);
            return this.srcImage.GetThumbnailImage(Width, Height, callback, IntPtr.Zero);
        }

        public static void MakeSquareImage(string fileName, string newFileName, int newSize)
        {
            Image image = Image.FromFile(fileName);
            int num = 0;
            int width = image.Width;
            int height = image.Height;
            if (width > height)
            {
                num = height;
            }
            else
            {
                num = width;
            }
            Bitmap bitmap = new Bitmap(newSize, newSize);
            try
            {
                Graphics graphics = Graphics.FromImage(bitmap);
                graphics.InterpolationMode = InterpolationMode.High;
                graphics.SmoothingMode = SmoothingMode.HighQuality;
                graphics.Clear(Color.Transparent);
                if (width < height)
                {
                    graphics.DrawImage(image, new Rectangle(0, 0, newSize, newSize), new Rectangle(0, (height - width) / 2, width, width), GraphicsUnit.Pixel);
                }
                else
                {
                    graphics.DrawImage(image, new Rectangle(0, 0, newSize, newSize), new Rectangle((width - height) / 2, 0, height, height), GraphicsUnit.Pixel);
                }
                SaveImage(bitmap, newFileName, GetCodecInfo("image/jpeg"));
            }
            finally
            {
                image.Dispose();
                bitmap.Dispose();
            }
        }

        public static void MakeThumbnail(string fileName, string newFile, int maxHeight, int maxWidth)
        {
            Image image = Image.FromFile(fileName);
            ImageFormat rawFormat = image.RawFormat;
            Size size = NewSize(maxWidth, maxHeight, image.Width, image.Height);
            Bitmap bitmap = new Bitmap(size.Width, size.Height);
            Graphics graphics = Graphics.FromImage(bitmap);
            graphics.CompositingQuality = CompositingQuality.HighQuality;
            graphics.SmoothingMode = SmoothingMode.HighQuality;
            graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
            graphics.DrawImage(image, new Rectangle(0, 0, size.Width, size.Height), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel);
            graphics.Dispose();
            EncoderParameters encoderParams = new EncoderParameters();
            long[] numArray = new long[] { 100L };
            EncoderParameter parameter = new EncoderParameter(Encoder.Quality, numArray);
            encoderParams.Param[0] = parameter;
            ImageCodecInfo[] imageEncoders = ImageCodecInfo.GetImageEncoders();
            ImageCodecInfo encoder = null;
            for (int i = 0; i < imageEncoders.Length; i++)
            {
                if (imageEncoders[i].FormatDescription.Equals("JPEG"))
                {
                    encoder = imageEncoders[i];
                    break;
                }
            }
            if (encoder != null)
            {
                bitmap.Save(newFile, encoder, encoderParams);
            }
            else
            {
                bitmap.Save(newFile, rawFormat);
            }
            image.Dispose();
            bitmap.Dispose();
        }

        public static void MakeThumbnailImage(string fileName, string newFileName, int maxWidth, int maxHeight)
        {
            Image original = Image.FromFile(fileName);
            Size newSize = ResizeImage(original.Width, original.Height, maxWidth, maxHeight);
            Image image2 = new Bitmap(original, newSize);
            original.Dispose();
            try
            {
                image2.Save(newFileName, GetFormat(fileName));
            }
            finally
            {
                original.Dispose();
            }
        }

        private static Size NewSize(int maxWidth, int maxHeight, int width, int height)
        {
            double num = 0.0;
            double num2 = 0.0;
            double num3 = Convert.ToDouble(width);
            double num4 = Convert.ToDouble(height);
            double num5 = Convert.ToDouble(maxWidth);
            double num6 = Convert.ToDouble(maxHeight);
            if ((num5 > 0.0) && (num6 <= 0.0))
            {
                if (num3 <= num5)
                {
                    num = num3;
                    num2 = num4;
                }
                else
                {
                    num = maxWidth;
                    num2 = (num * num4) / num3;
                }
            }
            else if ((num5 <= 0.0) && (num6 > 0.0))
            {
                if (num4 <= num6)
                {
                    num = num3;
                    num2 = num4;
                }
                else
                {
                    num2 = maxHeight;
                    num = (num2 * num3) / num4;
                }
            }
            else if ((num3 < num5) && (num4 < num6))
            {
                num = num3;
                num2 = num4;
            }
            else if ((num3 / num4) > (num5 / num6))
            {
                num = maxWidth;
                num2 = (num * num4) / num3;
            }
            else
            {
                num2 = maxHeight;
                num = (num2 * num3) / num4;
            }
            return new Size(Convert.ToInt32(num), Convert.ToInt32(num2));
        }

        private static Size ResizeImage(int width, int height, int maxWidth, int maxHeight)
        {
            int num4;
            int num5;
            decimal num = maxWidth;
            decimal num2 = maxHeight;
            decimal num3 = num / num2;
            decimal num6 = width;
            decimal num7 = height;
            if ((num6 > num) || (num7 > num2))
            {
                decimal num8;
                if ((num6 / num7) > num3)
                {
                    num8 = num6 / num;
                    num4 = Convert.ToInt32((decimal) (num6 / num8));
                    num5 = Convert.ToInt32((decimal) (num7 / num8));
                }
                else
                {
                    num8 = num7 / num2;
                    num4 = Convert.ToInt32((decimal) (num6 / num8));
                    num5 = Convert.ToInt32((decimal) (num7 / num8));
                }
            }
            else
            {
                num4 = width;
                num5 = height;
            }
            return new Size(num4, num5);
        }

        private static void SaveImage(Image image, string savePath, ImageCodecInfo ici)
        {
            EncoderParameters encoderParams = new EncoderParameters(1);
            encoderParams.Param[0] = new EncoderParameter(Encoder.Quality, 90L);
            image.Save(savePath, ici, encoderParams);
            encoderParams.Dispose();
        }

        public void SaveImage(int Width, int Height, ImageFormat imgformat)
        {
            if ((this.srcImage.Width > Width) || (this.srcImage.Height > Height))
            {
                Image.GetThumbnailImageAbort callback = new Image.GetThumbnailImageAbort(Thumbnail.ThumbnailCallback);
                Image image = this.srcImage.GetThumbnailImage(Width, Height, callback, IntPtr.Zero);
                this.srcImage.Dispose();
                image.Save(this.srcFileName, imgformat);
                image.Dispose();
            }
        }

        public void SaveThumbnailImage(int Width, int Height)
        {
            switch (Path.GetExtension(this.srcFileName).ToLower())
            {
                case ".png":
                    this.SaveImage(Width, Height, ImageFormat.Png);
                    return;

                case ".gif":
                    this.SaveImage(Width, Height, ImageFormat.Gif);
                    return;
            }
            this.SaveImage(Width, Height, ImageFormat.Jpeg);
        }

        public bool SetImage(string FileName)
        {
            this.srcFileName = Utils.GetMapPath(FileName);
            try
            {
                this.srcImage = Image.FromFile(this.srcFileName);
            }
            catch
            {
                return false;
            }
            return true;
        }

        public static bool ThumbnailCallback()
        {
            return false;
        }
    }
}

